//
//  SNSportChartSelectView.m
//  project
//
//  Created by 张淋 on 2019/5/31.
//  Copyright © 2019 黄建华. All rights reserved.
//

#import "SNSportChartSelectView.h"

@implementation SNSportChartSelectView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    self.layer.cornerRadius = 6;
    self.layer.masksToBounds = YES;
    self.backgroundColor = UIColorHEX(0xF2F4F5);
    
    NSArray *infoArray = [NSArray arrayWithObjects:SN_Text(@"本周"), SN_Text(@"本月"), SN_Text(@"三个月内"), nil];
    for (int i = 0; i < 3; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = SNFRAME(self.width / 3 *i+3, 1.5, self.width / 3-3, self.height-3);
        [button addTarget:self
                   action:@selector(buttonclick:)
         forControlEvents:UIControlEventTouchUpInside];
        button.tag = i + 101;
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [button setTitle:[infoArray objectAtIndex:i] forState:UIControlStateNormal];
        button.layer.cornerRadius = 6;
        button.layer.masksToBounds = YES;
        button.titleLabel.font = SNFONTMEDIUM(11);
        [self addSubview:button];
        button.titleLabel.adjustsFontSizeToFitWidth = YES;
        [button setBackgroundColor:[UIColor clearColor]];
        
        if (i == 0) {
            button.selected = YES;
            [button setBackgroundColor:UIColorHEX(0xFF955B)];
        } else {
            button.selected = NO;
        }
    }
}

- (void)buttonclick:(UIButton *)sender {
    SelectDateType type = SelectDateWeek;
    for (UIButton *button in self.subviews) {
        button.selected = NO;
        [button setBackgroundColor:[UIColor clearColor]];
    }
    if (sender.tag == 100) {
        type            = SelectDateWeek;
        sender.selected = YES;
        [sender setBackgroundColor:UIColorHEX(0xFF955B)];
    }
    if (sender.tag == 101) {
        type            = SelectDateMonth;
        sender.selected = YES;
        [sender setBackgroundColor:UIColorHEX(0xFF955B)];
    }
    if (sender.tag == 102) {
        type            = SelectDateThreeMonths;
        sender.selected = YES;
        [sender setBackgroundColor:UIColorHEX(0xFF955B)];
    }
    
    if (_selectType) {
        _selectType(type);
    }
}

@end
